/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.warehouse.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.utils.Func;
import com.ocm.warehouse.service.ILogisticsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.warehouse.entity.LogisticsDetail;
import com.ocm.warehouse.vo.LogisticsDetailVO;
import com.ocm.warehouse.wrapper.LogisticsDetailWrapper;
import com.ocm.warehouse.service.ILogisticsDetailService;
import com.ocm.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 *  控制器
 *
 * @author HQY
 * @since 2021-11-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/logisticsDetail")
@Api(value = "", tags = "接口")
public class LogisticsDetailController extends BladeController {

	private final ILogisticsDetailService logisticsDetailService;

	private final ILogisticsService logisticsService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入logisticsDetail")
	public R<LogisticsDetailVO> detail(LogisticsDetail logisticsDetail) {
		LogisticsDetail detail = logisticsDetailService.getOne(Condition.getQueryWrapper(logisticsDetail));
		return R.data(LogisticsDetailWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入logisticsDetail")
	public R<IPage<LogisticsDetailVO>> list(LogisticsDetail logisticsDetail, Query query) {
		IPage<LogisticsDetail> pages = logisticsDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(logisticsDetail));
		return R.data(LogisticsDetailWrapper.build().pageVO(pages));
	}




	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入logisticsDetail")
	public R<IPage<LogisticsDetailVO>> page(@ApiIgnore @RequestParam Map<String, Object> logisticsDetail, Query query) {
		//必填信息
		if(!logisticsDetail.containsKey("logisticsId_equal")){
			return R.fail("信息有误");
		}
		IPage<LogisticsDetailVO> pages = logisticsDetailService.selectLogisticsDetailPage(Condition.getPage(query),
			Condition.getQueryWrapper(logisticsDetail,LogisticsDetailVO.class).eq("d.is_deleted",0).groupBy("d.sku_id").orderByAsc("d.create_time"));
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入logisticsDetail")
	public R save(@Valid @RequestBody LogisticsDetail logisticsDetail) {
		return R.status(logisticsDetailService.save(logisticsDetail));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入logisticsDetail")
	public R update(@Valid @RequestBody LogisticsDetail logisticsDetail) {
		if(logisticsDetailService.countDetailStatusById(logisticsDetail.getId().toString())>0){
			return R.fail("已入库的运单不允许操作");
		}
		LogisticsDetail l=new LogisticsDetail();
		l.setId(logisticsDetail.getId());
		l.setStockNumber(logisticsDetail.getStockNumber());
		return R.status(logisticsDetailService.updateById(l));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入logisticsDetail")
	public R submit(@Valid @RequestBody LogisticsDetail logisticsDetail) {
		if(logisticsDetailService.countDetailStatusById(logisticsDetail.getId().toString())>0){
			return R.fail("已入库的运单不允许操作");
		}
		return R.status(logisticsDetailService.saveOrUpdate(logisticsDetail));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		if(logisticsDetailService.countDetailStatusById(ids)>0){
			return R.fail("已入库的运单不允许操作");
		}
		return R.status(logisticsDetailService.deleteLogic(Func.toLongList(ids)));
	}


}
